package com.project.imageinterference.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.project.imageinterference.dto.response.NewsRes;
import com.project.imageinterference.entity.News;
import com.project.imageinterference.exception.BusinessException;
import com.project.imageinterference.mapper.NewsMapper;
import com.project.imageinterference.service.NewsService;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import javax.annotation.Resource;
import java.util.List;

/**
 * <p>
 * 新闻表 服务实现类
 * </p>
 *
 * @author haochuanwan
 * @since 2025-02-22
 */
@Service
public class NewsServiceImpl extends ServiceImpl<NewsMapper, News> implements NewsService {

    @Resource
    private NewsMapper newsMapper;

    @Override
    public NewsRes getDefaultNews() {
        LambdaQueryWrapper<News> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.orderByDesc(News::getCreateTime);
        List<News> newsList = newsMapper.selectList(queryWrapper);
        if(CollectionUtils.isEmpty(newsList)){
            throw new BusinessException("新闻查询为空");
        }
        News news = newsList.get(0);
        NewsRes newsRes = new NewsRes();
        BeanUtil.copyProperties(news, newsRes);
        return newsRes;
    }
}
